package com.dongdong.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dongdong.mapper.TbGoodsMapper;
import com.dongdong.model.TbGoods;
import com.dongdong.model.TbGoodsExample;
import com.dongdong.service.GoodsMPService;
import com.dongdong.vo.GoodsSPUVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class GoodsMPServiceImpl extends ServiceImpl<TbGoodsMapper, TbGoods> implements GoodsMPService {

    @Autowired
    private TbGoodsMapper goodsMapper;

    @Override
    public List<TbGoods> getGoodsPage(Integer pageNum, Integer pageSize) {
        Page<TbGoods> page = new Page<>(pageNum, pageSize);
        goodsMapper.selectPage(page, null);
        List<TbGoods> records = page.getRecords();
        return records;
    }

    @Override
    public List<GoodsSPUVO> getGoodsDim(GoodsSPUVO goodsvo) {
        TbGoodsExample example = new TbGoodsExample();
        if (!StrUtil.isEmpty(goodsvo.getGoodsName())) {
            example.createCriteria().andGoodsNameLike(goodsvo.getGoodsName());

            List<TbGoods> tbGoods = goodsMapper.selectByExample(example);

            List<GoodsSPUVO> collect = tbGoods.stream().map(goodslist -> {
                GoodsSPUVO vo = new GoodsSPUVO();
                BeanUtils.copyProperties(goodslist, vo);
                return vo;
            }).collect(Collectors.toList());
            return collect;
        }
        return null;
    }
}
